package com.example.dy.dy_bigtask.action;

import com.example.dy.dy_bigtask.dao.SharperMapper;
import com.example.dy.dy_bigtask.pojo.Sharper;
import com.example.dy.dy_bigtask.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.awt.*;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Date;
import java.text.SimpleDateFormat;
@Controller
@RequestMapping("/sharper")
public class SharperAction {

    @Autowired
    SharperMapper sharperMapper;
    //18211170204丁垚
    @GetMapping("/password")
    public String renew(){
        return "pwd";
    }



    @RequestMapping(value = "/listsharpers")
    public String findAll(Model model,HttpSession session,
                          @RequestParam(value = "pageNow",required = false,defaultValue = "1") Integer pageNow,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                          @RequestParam(value = "sid",required = false,defaultValue = "-1") String sid_s,
                          @RequestParam(value = "type",required = false,defaultValue = "-1") String type,
                          @RequestParam(value = "sname",required = false,defaultValue = "-1") String sname) {
        PageHelper.startPage(pageNow,pageSize);
        List<Sharper> sharpers;
        Sharper param=null;
        Integer  sid=null;
        try {
            sid=Integer.parseInt(sid_s);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            sid = -1;
        }
        if(sid==-1 & sname.equals("-1") & type.equals("-1")){
            sharpers=sharperMapper.selectAll();
        }
        else{
            param= dealParam(sid, sname, type);
            System.out.println("param:"+param.getSid()+param.getsName()+ param.getType());
            sharpers=sharperMapper.searchSharper(param);
//            model.addAttribute("param",param);

        }
        System.out.println(sharpers.size());
        PageInfo<Sharper> pageInfo = new PageInfo<Sharper>(sharpers);
        dealSharpers(sharpers);
        if(pageNow ==1){
            pageInfo.setPrePage(1);
        }
        if(pageInfo.getNextPage() == 0){
            pageInfo.setNextPage(pageInfo.getPrePage()+1);
        }
        model.addAttribute("sharpers",sharpers);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow",pageNow);
        Object object = session.getAttribute("userinfo");
        if (null == object) {
            return  "user_part";
        }
        return "provider_part";
    }

    private List<Sharper> dealSharpers(List<Sharper> sharpers) {
        String[] states={"空闲","","使用中","报废"};
        for (Sharper sharper:sharpers) {
            if(sharper.getsPosition()!=0){
            sharper.setMechine(sharperMapper.findMechineBySid(sharper));
            }
            else{sharper.setMechine("仓库");}
            sharper.setUsername(sharperMapper.findUsernameBysid(sharper));
            sharper.setStatename(states[sharper.getState()]);
        }
        return sharpers;
    }

    private Sharper dealParam(Integer sid ,String sname,String type) {
        Sharper param=new Sharper();
        param.setSid(sid);
        param.setsName(sname);
        param.setType(type);
        return param;
    }


    @RequestMapping(value = "/back/{sid}")
    @ResponseBody
    public Integer delEmpById(@PathVariable Integer sid){
        System.out.println("归还模具："+sid);
        Date date = new Date();// new Date()为获取当前系统时间
        Sharper sharper = new Sharper();
        sharper.setSid(sid);
        sharper.setDate(date);
        return sharperMapper.backSharper(sharper);

    }

    @RequestMapping(value = "/discard/{sid}")
    @ResponseBody
    public Integer discardSharperById(@PathVariable Integer sid ,HttpSession session){
        System.out.println("废除模具："+sid);
        Date date = new Date();// new Date()为获取当前系统时间
        Sharper sharper = new Sharper();
        sharper.setSid(sid);
        sharper.setDate(date);
        User userinfo = (User) session.getAttribute("userinfo");
        sharper.setUser(userinfo.getId());
        return sharperMapper.discardSharper(sharper);
    }


    @RequestMapping(value = "/addSharper/{addnum}")
    @ResponseBody
    public Integer addSharper(Sharper sharper,HttpSession session,@PathVariable Integer addnum ){
        Date date = new Date();// new Date()为获取当前系统时间
        sharper.setDate(date);
        User userinfo = (User) session.getAttribute("userinfo");
        sharper.setUser(userinfo.getId());
        Integer result=0;
        for(int i=0;i<addnum;i++){
             result += sharperMapper.addSharper(sharper);
        }
        return result;
    }

    @RequestMapping(value = "/borrow")
    @ResponseBody
    public Integer borrowSharper(Sharper sharper){
        Date date = new Date();// new Date()为获取当前系统时间
        sharper.setDate(date);
        return sharperMapper.borrowSharper(sharper);
    }

    @RequestMapping(value = "/allocate")
    @ResponseBody
    public Integer allocateSharper(Sharper sharper,HttpSession session){
        Date date = new Date();// new Date()为获取当前系统时间
        sharper.setDate(date);
        User userinfo = (User) session.getAttribute("userinfo");
        sharper.setUser(userinfo.getId());
        if(sharper.getsPosition()==0){
            return sharperMapper.backSharper(sharper);
        }
        return sharperMapper.borrowSharper(sharper);
    }

}
